//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CA = ComponentArt.Web.UI;
using ModalLink = CommunityServer.ControlPanel.Controls.ModalLink;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Blogs
{
	/// <summary>
	/// Summary description for PostEditorPage.
	/// </summary>
	public class LinksPage : BaseBlogPage
	{
        protected Repeater LinkList;
        protected DropDownList CategoryList;
        protected int CurrentCategoryID;
		protected CommunityServer.ControlPanel.Controls.ControlPanelSelectedNavigation SelectedNavigation1;
		protected CommunityServer.Controls.MPContent DescriptionRegion;
		protected CommunityServer.Controls.Modal Modal1;
		protected CommunityServer.ControlPanel.Controls.FormLabel Formlabel1;
		protected CommunityServer.Controls.ModalLink ModalLink1;
		protected CommunityServer.Controls.MPContent TaskRegion;
		protected CommunityServer.Controls.MPContainer MPContainer;
		protected CommunityServer.ControlPanel.Controls.ResourceControl Resourcecontrol2;
        protected string CreateNewLink;

		override protected void OnInit(EventArgs e)
		{
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
		}

		private void Page_Load(object sender, EventArgs e)
		{
            JavaScript.RegisterRefresh(this);
            	ArrayList list = LinkCategories.GetCategories(this.CurrentWeblog.SectionID,true,false);
                if(list.Count > 0)
                {

                    foreach(LinkCategory category in list)
                    {
                        CategoryList.Items.Add(new ListItem(category.Name,category.LinkCategoryID.ToString()));
                    }

                    if(Request.QueryString["CategoryID"] != null)
                    {
                        ListItem li = CategoryList.Items.FindByValue(Request.QueryString["CategoryID"]);
                        if(li != null)
                            li.Selected = true;

                        
                        BindLinks(Int32.Parse(Request.QueryString["CategoryID"]), null);
                    }
                    else
                    {
                        LinkCategory lc = list[0] as LinkCategory;
                        BindLinks(lc.LinkCategoryID, null);
                    }

                }
                else
                {
                    CategoryList.Style.Add("display","none");
                }

                CategoryList.Attributes.Add("onchange", "selectCategory();");


		}

        void BindLinks(int CategoryID, HtmlTextWriter writer)
        {
            CurrentCategoryID = CategoryID;
            ArrayList links = Links.GetLinks(CategoryID,true,false);
            LinkList.DataSource = links;
            LinkList.DataBind();

            ModalLink link = new ModalLink();
            link.Height=300;
			link.CssClass = "CommonTextButton";
            link.Width = 400;
            link.Callback = "refreshCallback";
            link.Url = "LinkForm.aspx?CategoryID=" + CategoryID.ToString();
            link.Text = ResourceManager.GetString("CP_Blog_Links_NewLink");
            link.ID = "NewLink";

			bool isCallBack = true;
			StringBuilder sb = new StringBuilder();
			if (writer == null)
			{
				isCallBack = false;
				StringWriter sw = new StringWriter(sb);
				writer = new HtmlTextWriter(sw);
			}
            link.RenderControl(writer);

            link.ID = "EditCategory";
			link.CssClass = "CommonTextButton";
            link.Url = "LinkCategoryForm.aspx?CategoryID=" + CategoryID.ToString();
            link.Text = ResourceManager.GetString("CP_Blog_Links_EditCategory");
            link.Callback = "reloadCategories";
            link.RenderControl(writer);

            link.Height = 150;
			link.CssClass = "CommonTextButton";
            link.Width = 300;
            link.ID = "DeleteCategory";
            link.Url = "BlogCommand.aspx?Command=" + typeof(DeleteLinkCategoryCommand).FullName + "&CategoryID=" + CategoryID.ToString();
            link.Text = ResourceManager.GetString("CP_Blog_Links_DeleteCategory");
            link.Callback = "removeCategory";
            link.RenderControl(writer);

			CreateNewLink = writer.ToString();

			if (isCallBack)
				CreateNewLink = writer.ToString();
			else
				CreateNewLink = sb.ToString();
        }

    }
}
